package pl.edu.agh.uddiProxy.init;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import pl.edu.agh.uddiProxy.dao.DAOHelper;
import pl.edu.agh.uddiProxy.types.Authority;
import pl.edu.agh.uddiProxy.types.User;

public class DatabaseInitializer implements ServletContextListener {
	public void createAdmin() {
		if (!DAOHelper.getUserDAO().exists("admin")) {
			Set<Authority> set = new HashSet<Authority>();
			set.add(Authority.getAuthority(Authority.ADMIN));
			set.add(Authority.getAuthority(Authority.USER));
			set.add(Authority.getAuthority(Authority.COMMON));
			set.add(Authority.getAuthority(Authority.ANONYMOUS));
			User user = new User("admin", "Administrator", "", "admin", "admin@localhost.com", set);
			DAOHelper.getUserDAO().create(user);
		}
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		
	}

	public void contextInitialized(ServletContextEvent arg0) {
		createAdmin();
	}
}
